<template>
  <transition name="fade">
    <button 
      v-show="visible" 
      class="back-to-top"
      @click="scrollToTop"
      aria-label="返回顶部"
    >
      <span class="tooltip">返回顶部</span>
      <i class="fas fa-arrow-up"></i>
    </button>
  </transition>
</template>

<script>
export default {
  name: 'BackToTop',
  
  data() {
    return {
      visible: false,
      scrollThreshold: 100, // 更低的显示阈值
      isScrolling: false
    }
  },
  
  methods: {
    handleScroll() {
      // 使用 requestAnimationFrame 优化滚动性能
      if (!this.isScrolling) {
        window.requestAnimationFrame(() => {
          const scrollTop = window.pageYOffset || document.documentElement.scrollTop
          const windowHeight = window.innerHeight
          
          // 当滚动超过一屏高度的 20% 时显示
          this.visible = scrollTop > windowHeight * 0.2
          this.isScrolling = false
        })
        this.isScrolling = true
      }
    },
    
    scrollToTop() {
      // 使用平滑滚动动画
      const duration = 800
      const start = window.pageYOffset
      const startTime = performance.now()
      
      const animateScroll = (currentTime) => {
        const elapsed = currentTime - startTime
        const progress = Math.min(elapsed / duration, 1)
        
        // easeInOutQuad 缓动函数
        const easeProgress = progress < 0.5
          ? 2 * progress * progress
          : 1 - Math.pow(-2 * progress + 2, 2) / 2
        
        window.scrollTo(0, start * (1 - easeProgress))
        
        if (progress < 1) {
          requestAnimationFrame(animateScroll)
        }
      }
      
      requestAnimationFrame(animateScroll)
    }
  },
  
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="scss" scoped>
.back-to-top {
  position: fixed;
  bottom: 3rem;
  right: 1.5rem;
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  background: var(--accent-color);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-speed);
  z-index: 99;
  opacity: 0.8;
  
  &:hover {
    transform: translateY(-3px);
    opacity: 1;
    
    .tooltip {
      opacity: 1;
      transform: translateX(-50%) translateY(0);
    }
  }
  
  i {
    font-size: 1.2rem;
    transition: transform 0.3s ease;
  }
  
  &:hover i {
    transform: translateY(-2px);
  }
  
  .tooltip {
    position: absolute;
    top: -2rem;
    left: 50%;
    transform: translateX(-50%) translateY(5px);
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 0.4rem 0.8rem;
    border-radius: 0.25rem;
    font-size: 0.875rem;
    white-space: nowrap;
    opacity: 0;
    transition: all var(--transition-speed);
    pointer-events: none;
  }
}

// 动画效果
.fade-enter-active,
.fade-leave-active {
  transition: all 0.4s ease-in-out;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
  transform: scale(0.8) translateY(10px);
}

@media (max-width: 768px) {
  .back-to-top {
    bottom: 2.5rem;
    right: 1.5rem;
    width: 2.8rem;
    height: 2.8rem;
    opacity: 0.8;
    
    i {
      font-size: 1rem;
    }
    
    .tooltip {
      display: none;
    }
    
    &:active {
      transform: scale(0.95);
    }
  }
}
</style> 